<template>
  <!-- 风险复核新增编辑查看 -->
  <a-form-model
    ref="ruleForm"
    :model="form"
    :label-col="{ span: 6 }"
    :wrapper-col="{ span: 18 }"
    :rules="state != 3 ? rules : {}"
  >
    <a-spin :spinning="loading">
      <!-- 表格 -->
      <a-card :bordered="false">
        <title-name :title="state == 3 ? '风险复核': '基本信息'" />
        <custom-form
          :form-list="formList"
          :form="form"
          :edit-model="state == 3 ? 'V' : ''"
        >
          <template #checkList>
            <a-form-model-item
              v-for="(item, n) in form.riskReviewTree[0].children"
              :key="n"
              :label="item.name"
              prop="type"
            >
              <a-checkbox-group
                v-model="item.checkedIds"
                :disabled="state == 3 ? true : false"
              >
                <div
                  v-for="(items, m) in item.children"
                  :key="items.id"
                >
                  <a-checkbox
                    :value="items.id"
                    @change="onChange(items, m)"
                  >
                    {{ items.name }}
                  </a-checkbox>
                </div>
              </a-checkbox-group>
            </a-form-model-item>
          </template>
        </custom-form>
      </a-card>
      <!--附件-->
      <div>
        <attachment
          :attachment="form.fileInfoList"
          :disabled="state == 3 ? false : true"
          @change="(e) => (form.fileInfoList = e)"
        />
      </div>
    </a-spin>
    <!-- 页脚操作栏 -->
    <div class="btn_box page-btn-right-top">
      <a-button
        v-if="state != 3"
        :loading="loadingSav"
        type="primary"
        class="button_right"
        @click="handleSubmit(1)"
      >
        保存草稿
      </a-button>
      <a-button
        v-if="state != 3"
        :loading="loadingSub"
        type="primary"
        class="button_right"
        @click="handleSubmit(2)"
      >
        提交
      </a-button>
      <a-button
        v-show="state != 3"
        type="primary"
        @click="handleActiveClose"
      >
        关闭
      </a-button>
    </div>
    <selectApprover :businessId="businessId"  :processConfig="setProcessConfig"></selectApprover>
  </a-form-model>
</template>

<script>
  import BaseEditPage from '@/views/baseEditPage' // 编辑详情页基础配置
  import { create, dangerMudel, getLabels, detail, processDetail, print } from '@/api/legal/dangerReview/dangerreview'
  import attachment from './attachment' // 附件
  export default {
    extends: new BaseEditPage(),
    components:{
      attachment,
    },
    props: {
      //提交：2；查看：3
      //editModel:操作状态:0 查看;1 编辑;2 新增
      // state: {
      //   type: [String, Number],
      //   default() {
      //     return 1
      //   }
      // },
      process: {
        type: [String, Number],
      },
      baseForm: {
        type: Object
      }
    },
    data() {
      return {
        loading: false,
        loadingSav: false,
        loadingSub: false,
        state: this.$route.query.state,
        // editModel: this.$route.query.editModel, // editModel操作状态:0 查看;1 编辑;2 新增
        typeArr:[],
        form: {
          riskReviewTree: [{children:[]}],
          fileInfoList:[],
          name: '中国建筑第八工程局有限公司西南分公司',
          projectName: this.baseForm.projectName?this.baseForm.projectName:'',
          otherParty: this.baseForm.otherParty?this.baseForm.otherParty:'',
          generalContractId: this.baseForm.baseId?this.baseForm.baseId:'',
          amount: this.baseForm.amount?this.baseForm.amount:''
        }, // 表单
        rules: {
          name: [{ required: true, whitespace: true, message: '自动带出实施单位', trigger: 'change' }],
          projectName: [{ required: true, whitespace: true, message: '自动带出项目名称', trigger: 'change' }],
          otherParty: [{ required: true, whitespace: true, message: '自动带出建设单位', trigger: 'change' }],
          amount: [{ required: true, message: '自动带出合同额(万元)', trigger: 'change' }],
          contractTypeId: [{ required: true, whitespace: true, message: '请选择合同类型', trigger: 'change' }],
          preApproval: [{ required: true, whitespace: true, message: '请选择是否提供前置审批通过的资料', trigger: 'change' }],
          substantive: [{ required: true, whitespace: true, message: '请选择是否属于局实质评审合同', trigger: 'change' }],
        }, // 校验规则
        init: false, // 初始化控件
         //获取流程配置值
        processConfig: null,
        //点击提交发送流程配置值
        setProcessConfig: null,
        businessId: null
      }
    },
    computed: {
      formList() {
        let statusBool=[
            {
              label:'是',
              value: 'true'
            },
            {
              label:'否',
              value: 'false'
            },
          ]
        return [
          {
            keyName: 'name',
            type: 'a-input',
            disabled:true,
            label: '实施单位',
            span:24
          },
          {
            keyName: 'projectName',
            type: 'a-input',
            label: '项目名称',
            disabled:true,
          },
          {
            keyName: 'otherParty',
            type: 'a-input',
            label: '建设单位',
            disabled:true,
          },
          {
            keyName: 'amount',
            type: 'a-input',
            label: '合同额(万元)',
            disabled:true,
          },
          {
            keyName: 'contractTypeId',
            viewName: 'contractTypeName',
            label: '合同类型',
            type: 'a-select',
            data: this.typeArr,
            replaceFields: {
              value: 'id', // 默认为value
              label: 'name' // 默认为label
            }
          },
          {
            keyName: 'preApproval',
            viewName: 'preApprovalName',
            label: '是否提供前置审批通过的资料',
            type: 'a-select',
            data: statusBool
          },
          {
            keyName: 'substantive',
            viewName: 'substantiveName',
            label: '是否属于局实质评审合同',
            type: 'a-select',
            data: statusBool,
          },
          {
            slot: true,
            keyName: 'checkList',
            // label: '多选',
            span: 24,
          },
        ]
      },
    },
    watch: {
      baseForm(val){
        if(val){
          if(this.process){
            this.form={
              ...this.form,
              ...val,
              preApprovalName:val.preApproval? '是' : '否',
              substantiveName:val.substantive? '是' : '否',
              riskReviewTree: val.riskReviewTreeVO,
            }
          }
        }
      }
    },
   async mounted(){
      /**
       * 分类
       * 合同类型
       **/
      getLabels({query:{type:'15'}}).then((res)=>{
        this.typeArr=res.data
      }).catch(()=>{
      });

      this.handleModel();
      /**
       * 不为添加时请求详情
       * */
      if(this.process){
        this.state = '3';
      }else {
        this.handleDetail()
      }
     //获取通过获取流程配置值
      this.processConfig = await this.$getConfigFn('legal_risk_review') 
    },
    methods: {
      onChange(items) {
        if(items.choose){
          items.choose = !items.choose
        }else {
          items.choose = true
        }
      },
      handleDetail(val){
        let that = this
        // const api=[processDetail, detail][val-1]
        that.loading = true
        detail({id:this.$route.query.id})
          .then((res) => {
            let data = res.data
            that.loading = false
            if(res.data){
              that.form = {
                ...res.data,
                id:res.data.id,
                preApprovalName:data.preApproval? '是' : '否',
                substantiveName:data.substantive? '是' : '否',
                preApproval:data.preApproval.toString(),
                substantive:data.preApproval.toString(),
                riskReviewTree: data.riskReviewTreeVO,
                fileInfoList:data.fileInfoList,
                contractTypeId:data.contractTypeId,
              }
            }
            this.$nextTick(() => {
              this.init = true
            })
          })
          .catch(() => {
            that.loading = false
          })
        // } else {
        //   this.$nextTick(() => {
        //     this.init = true
        //   })
        // };
      },
      /**
       * 风险复核多选模板
       **/
      handleModel(){
        dangerMudel().then((res) => {
          this.form.riskReviewTree = res.data
        }).catch(() => {});

      },
  /**
       * 校验表单
       */
      handleSubmit(val) {
        if(val =='2'){
          // 提交表单
          // 校验表单
          this.$refs.ruleForm.validate((e) => {
            if (e) {
              this.handleSubmitApi(val)
            } else {
              this.$message.warning("请填写完所有必填项信息")
            }
          })
        }else{
          //保存表单
          this.handleSubmitApi(val)
        }
      },
      /**
       * 提交表单
       */
      handleSubmitApi(val){
        let that = this
        that.setProcessConfig = null; 
        that.loading = true
        if(val=='2'){
          that.loadingSub = true
        }else {
          that.loadingSav = true
        }
        // let api = [update, create][val - 1] // 保存：1； 提交：2
        let formData = that.$clone(that.form)
        let form={
          ...formData,
          riskReviewTree: JSON.parse(JSON.stringify(this.form.riskReviewTree).replace(/children/g,"childrenDTO")),
          flielist: formData.fileInfoList?formData.fileInfoList.map(item=>item.commonId):[],
          preApproval: formData.preApproval === 'false' ? false : true,
          substantive: formData.substantive === 'false' ? false : true,
          preApprovalName: formData.preApproval === 'false' ? '否' : '是',
          substantiveName: formData.substantive === 'false' ? '否' : '是',
          status: val=='2' ? 'SUBMITTED' : 'DRAFT', //结果已提交未反馈：NO_FEEDBACK； 待提交：DRAFT
        }
         this.submitApprovalConfirm(val == '2'?this.processConfig:6,(flag)=>{
            if(!flag) {
                this.loading = false
                return
              }
          create(form)
          .then((res) => {
            that.loading = false
            if(val=='2'){
              that.$message.success('提交成功！')
              that.loadingSub = false
              that.businessId = res.data.id;
              that.setProcessConfig = this.processConfig;
            }else {
              that.$message.success('保存成功！')
              that.loadingSav = false
              that.$multiTab.close()
            }
          })
          .catch(() => {
            that.loading = false
            if(val=='2'){
              that.loadingSub = false
            }else {
              that.loadingSav = false
            }
          })
            })

      },
    }
  }
</script>

<style lang="less" scoped>
    /deep/ .ant-form-item {
        .ant-form-item-label {
            height: 28px;
            width: 200px;
            line-height: 22px;
            padding: 6px 4px 0 0;
            text-align: right;
            white-space: pre-wrap;
            overflow: visible;
        }
        .ant-form-item-control-wrapper {
            width: calc(100% - 200px);
        }
    }
    .file_prompt {
        color: #999;
    }
    /deep/ .ant-form-item label{
        /*display: block;*/
        margin-left: 10px;
        margin-bottom: 10px;
    }
</style>
